package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.AppType;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.ao.UserType;
import com.ygqh.baby.mapper.WxAttentionMapper;
import com.ygqh.baby.po.WxAttention;
import com.ygqh.baby.po.WxAttentionExample;
import com.ygqh.baby.service.WxAttentionService;
import com.ygqh.baby.utils.DateConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * @author Ss
 */
@Service
public class WxAttentionServiceImpl implements WxAttentionService {

    @Autowired
    private WxAttentionMapper wxAttentionMapper;

    @Override
    public List<WxAttention> find() {
        return wxAttentionMapper.selectByExample(null);
    }

    @Override
    public int findAccount(String openId) {
        WxAttentionExample example = new WxAttentionExample();
        WxAttentionExample.Criteria criteria = example.createCriteria();
        criteria.andOpenIdEqualTo(openId);
        return wxAttentionMapper.countByExample(example);
    }

    @Override
    public WxAttention findById(Long id) {
        return wxAttentionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Long[] ids, Long handlerId) {
        Assert.notEmpty(ids);
        int count = 0;
        for (Long id : ids) {
            count += wxAttentionMapper.deleteByPrimaryKey(id);
        }
        return count;
    }

    @Override
    public int save(WxAttention wxAttention) {
        return wxAttentionMapper.insertSelective(wxAttention);
    }

    @Override
    public int update(WxAttention wxAttention) {
        return wxAttentionMapper.updateByPrimaryKeySelective(wxAttention);
    }

    @Override
    public ResultSet<WxAttention> search(QueryInfo queryInfo, String q) {
        int count = wxAttentionMapper.countSuper(q);
        if (count == 0) {
            return new ResultSet<>();
        }
        List<WxAttention> list = wxAttentionMapper.selectSuper(queryInfo, q);
        return new ResultSet<>(count, list);
    }

    @Override
    public int deleteSoft(Long[] ids, Long handlerId) {
        return 0;
    }

    @Override
    public int saveOrUpdate(WxAttention wxAttention) {
        int r;
        if (wxAttention.getId() != null) {
            r = this.update(wxAttention);
        } else {
            r = this.save(wxAttention);
        }
        return r;
    }

    @Override
    public List<WxAttention> selectAttentionList(Date startDate, Integer ygSeoId, String platformNo) {
        return wxAttentionMapper.selectAttentionList(startDate, ygSeoId, platformNo);
    }

    @Override
    public List<WxAttention> selectAllAttention(Date startDate, Integer ygSeoId, String platformNo) {
        return wxAttentionMapper.selectAllAttention(startDate, ygSeoId, platformNo);
    }

    @Override
    public List<WxAttention> findListGroupByOpenId(AppType recordFrom, Date startDate, int count, String platformNo) {
        return wxAttentionMapper.selectListGroupByOpenId(recordFrom, startDate, count, platformNo);
    }

    @Override
    public int updateUpdateTime(Long id) {
        return wxAttentionMapper.updateUpdateTimeById(id);
    }

    @Override
    public WxAttention findByOpenId(String openId) {
        return wxAttentionMapper.selectByOpenId(openId);
    }

    @Override
    public List<WxAttention> findAttentionListNoOrder(Date startDate, Integer ygSeoId, String platformNo) {
        return wxAttentionMapper.selectAttentionListNoOrder(startDate, ygSeoId, platformNo);
    }

    @Override
    public List<WxAttention> findByUser(Integer level, UserType userType, Date startDate, Integer ygSeoId, String platformNo) {
        return wxAttentionMapper.selectByUser(level, userType == null ? null : userType.name(), startDate, ygSeoId, platformNo);
    }

    @Override
    public List<WxAttention> findByNewCustomer(Date startDate, Integer ygSeoId, String platformNo) {
        return wxAttentionMapper.selectByNewCustomer(startDate, ygSeoId, platformNo);
    }

    @Override
    public WxAttention findValidFormIdRecordByOpenId(String openId, Integer ygSeoId) {

        return wxAttentionMapper.selectValidFormIdRecordByOpenId(openId, ygSeoId, DateConvertUtils.addDay(new Date(), -7));
    }

    @Override
    public int updateRecordUsed(Long id) {
        WxAttention wxAttention = new WxAttention();
        wxAttention.setId(id);
        wxAttention.setUpdateTime(new Date());
        wxAttention.setUseStatus(1);
        return wxAttentionMapper.updateByPrimaryKeySelective(wxAttention);
    }
}
